package com.sakura.takeaway.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sakura.takeaway.common.CustomException;
import com.sakura.takeaway.dto.SetmealDto;
import com.sakura.takeaway.entity.Setmeal;
import com.sakura.takeaway.entity.SetmealDish;
import com.sakura.takeaway.mapper.SetmealMapper;
import com.sakura.takeaway.service.SetmealDishService;
import com.sakura.takeaway.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Sakura
 * @Date 2022-07-10 15:40
 */
@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增菜品，同时需要保存套餐和菜品的关联信息
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {

        //保存套餐的基本信息，操作setmeal，执行insert操作
        this.save(setmealDto);

        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //保存套餐和菜品的关联信息，操作setmeal_dish，执行insert操作
        setmealDishService.saveBatch(setmealDishes);

    }

    /**
     * 删除套餐，同时删除套餐和菜品的关联数据
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {

        //select count(*) from setmeal where id in (1,2,3) and status = 1
        //查询套餐状态
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper =new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.in(Setmeal::getId,ids);
        setmealLambdaQueryWrapper.eq(Setmeal::getStatus,1);

        int count = this.count(setmealLambdaQueryWrapper);
        if (count>0){
            //如果是在售卖状态，不能删除，抛出一个业务异常
            throw new CustomException("当前套餐为售卖状态，不能删除");

        }

        //如果可以删除，先删除套餐表setmeal中的数据
        this.removeByIds(ids);

        //delete from setmeal_dish where setmealId in (1,2,3)
        LambdaQueryWrapper<SetmealDish> dishLambdaQueryWrapper =new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.in(SetmealDish::getDishId,ids);
        //删除关系表中的数据 ----setmeal_dish
        setmealDishService.remove(dishLambdaQueryWrapper);
    }

    /**
     * 根据id查询套餐信息和对应的套餐菜品信息
     * @param id
     * @return
     */
    @Override
    @Transactional
    public SetmealDto getByIdWithDish(Long id) {

        //查询菜品基本信息，从setmeal表中查询
        Setmeal setmeal = this.getById(id);

        //对象拷贝
        SetmealDto setmealDto =new SetmealDto();
        BeanUtils.copyProperties(setmeal,setmealDto);

        //查询当前套餐的菜品信息，从setmeal_dish表中查询
        LambdaQueryWrapper<SetmealDish> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getDishId,setmeal.getId());

        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(list);

        return setmealDto;
    }

    /**
     * 更新菜品信息和对应的口味信息
     * @param setmealDto
     */
    @Transactional
    @Override
    public void updateWithDish(SetmealDto setmealDto) {

        //更新setmeal表的基本信息
        this.updateById(setmealDto);

        //清理当前套餐对应的菜品数据 -------setmeal_dish 表的delete 操作
        LambdaQueryWrapper<SetmealDish> queryWrapper =new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getDishId,setmealDto.getId());

        setmealDishService.remove(queryWrapper);

        //添加当前提交过来的菜品表 ------ setmeal_dish表的insert 操作
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();

        setmealDishes=setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        setmealDishService.saveBatch(setmealDishes);

    }
}
